package controllers;

import static play.data.Form.form;
import logger.LogSchedular;
import models.Account;
import models.Role;
import play.*;
import play.data.Form;
import play.data.format.Formats;
import play.data.validation.Constraints;
import play.mvc.*;

import views.html.*;

public class Application extends Controller {
	// -- Authentication
    public static class Login {
    	
    	@Constraints.Required
        @Formats.NonEmpty
        public String name;
    	
    	@Constraints.Required
        @Formats.NonEmpty
        public String password;
        
        public String validate() {
        	Account user = Account.find.where().eq("name", name).eq("password", password).findUnique();
            if(user == null) {
            	return play.i18n.Messages.get("login.error.invalidpwd");
            } else {
            	LogSchedular.logLogin(user);
            	updateUserSession(user.name);
            }
            return null;
        }
        
    }
    
    public static class Register {
    	public String name = ""; //init with empty string to avoid template Null point error
    	public String password = "";
    	public String validate() {
    		if (name.isEmpty()) {
    			return "用户名不能为空";
    		}
   
    		if(Account.findByName(name) != null) {
    			return "该用户名已经被注册";
    		}
    		
    		if (password.isEmpty()) {
    			return "密码不能为空";
    		}
         
       	 	//save to db
       	 	Account user = new Account();
       	 	user.name = name;
       	 	user.password = password;
       	 	user.save();
         
       	 	updateUserSession(user.name);
       	 
    		return null;
    	}
    }
   
    
    public static Result login() {
    	return ok(views.html.accounts.login.render(form(Login.class)));
    	//return ok();
    }
    
    /**
     * Handle logi/n form submission.
     */
    public static Result authenticate() {
        Form<Login> loginForm = form(Login.class).bindFromRequest();
        if(loginForm.hasErrors()) {
            return badRequest(views.html.accounts.login.render(loginForm));
        } else {
            return redirect(
                routes.Projects.index()
            );
        }
    }

    /**
     * Logout and clean the session.
     */
    public static Result logout() {
        session().clear();
        flash("success", "您已经退出");
        return redirect(
            routes.Application.login()
        );
    }
    
    public static Result register() {
    	return ok(views.html.accounts.register.render(form(Register.class)));
    	//return ok();
    }
    
    public static Result registerNew() {
    	 Form<Register> registerForm = form(Register.class).bindFromRequest();
         if(registerForm.hasErrors()) {
        	 return badRequest(views.html.accounts.register.render(registerForm));
         } else {             
             return redirect(
                 routes.Projects.index()
             );
         }
    }
    
    public static Result javascriptRoutes() {
        response().setContentType("text/javascript");
        return ok(
            Routes.javascriptRouter("jsRoutes",
            		controllers.routes.javascript.Projects.index(),
            		controllers.routes.javascript.Accounts.add()
            )
        );
    }
    
    private static void updateUserSession(String name) {
    	session("name", name);
    }
}
